package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	import org.cove.ape.*;
	//import org.cove.ape


	[SWF(width="700", height="500", backgroundColor="#000000")] 
	public class TechDemo extends Sprite {
		
		private static var centerX:Number = 350; // THESE TWO PROPERTIES DEFINED IN ACCORDANCE WITH ABOVE
		private static var centerY:Number = 250;
		
		private static var colA:uint = 0xffffff;
		private static var colB:uint = 0xff0000;
		private static var colC:uint = 0x00ff00;
		private static var colD:uint = 0x0000ff;
		private static var colE:uint = 0xaaaaaa;
		private static var surfaces:Surfaces_Tech;
	
	     private var block:Block;
			
		public function TechDemo() {
		
			// set up the events, main loop handler, and the engine. you don't have to use
			// enterframe. you just need to call the ApeEngine.step() and ApeEngine.paint() 
			// wherever and however you're handling your program cycle.
			stage.frameRate = 48;
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			addEventListener(Event.ENTER_FRAME, run);
			
			// Initialize the engine. The argument here is the time step value. 
			// Higher values scale the forces in the sim, making it appear to run
			// faster or slower. Lower values result in more accurate simulations.
			APEngine.init(1/2);
			
			// set up the default diplay container
			APEngine.container = this;
			
			// gravity -- particles of varying masses are affected the same
			APEngine.addMasslessForce(new Vector(0, 5));
			
			// groups - all these classes extend group
			surfaces = new Surfaces_Tech(colB, colC, colD, colE);
			surfaces.addEventListeners(CollisionEvent.COLLIDE, check_coll);
			APEngine.addGroup(surfaces);
			
			block = new Block(colA);
			APEngine.addGroup(block);
			
			// determine what collides with what.
			block.addCollidableList(new Array(surfaces));
			
			//var tempPart:RectangleParticle = new RectangleParticle(350, 250, 20, 20, 0, true);
			addEventListener(CollisionEvent.COLLIDE, check_coll);
		}	
		
		
		
		function run(evt:Event):void {
			APEngine.step();
			APEngine.paint();
			//rotator.rotateByRadian(.02);
		}
		
		
				
		function keyDownHandler(event:KeyboardEvent):void {

			if (event.keyCode == 39) {  // RIGHT
				block.moveRight();
			} 
			if (event.keyCode == 37) { // LEFT
				block.moveLeft();
			}
			if (event.keyCode == 38) { //UP
				// not touching ground?
				block.jump();
			}
			
			if (event.keyCode == 65) { // A - rotate CCW
				rotateLevel(-90);

			}
			
			if (event.keyCode == 68) { // D - rotate CW
				rotateLevel(90);
			}
		}
		
		//change
		function keyUpHandler(keyEvt:KeyboardEvent):void {
	 
		}
		
		
		function rotateLevel(degrees:Number):void {
			surfaces.rotate(degrees, centerX, centerY); // rotate level
			block.rotate(degrees, centerX, centerY); // rotate player character
		}
		
		

		function check_coll(e:CollisionEvent):void {
			trace("collision!");
		    var collided = e.collidingItem.sprite.name;
		    if (collided=="footParticle") {
		        trace("  on floor!");
		    }
		}

	}
}




